from django import forms
from .models import Book, Author, BookInstance
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
import datetime

class RenewBookForm(forms.Form):
    renewal_date = forms.DateField(help_text="输入新的归还日期 (默认为3周后)")

    def clean_renewal_date(self):
        data = self.cleaned_data['renewal_date']
        
        # 检查日期是否在过去
        if data < datetime.date.today():
            raise ValidationError(_('无效的日期 - 续借日期不能在过去'))
        
        # 检查日期是否在允许的范围内（例如，最多4周）
        if data > datetime.date.today() + datetime.timedelta(weeks=4):
            raise ValidationError(_('无效的日期 - 续借日期不能超过4周'))
        
        return data

class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ['title', 'author', 'summary', 'isbn', 'genre', 'language']
        widgets = {
            'summary': forms.Textarea(attrs={'rows': 4}),
            'isbn': forms.TextInput(attrs={'placeholder': '例如：978-3-16-148410-0'}),
        }
        labels = {
            'title': '书名',
            'author': '作者',
            'summary': '简介',
            'isbn': 'ISBN',
            'genre': '类型',
            'language': '语言',
        }
        help_texts = {
            'isbn': '13位 ISBN 号码',
            'summary': '请输入图书的简要描述',
        } 